package com.software.car.service;

import com.github.pagehelper.PageHelper;
import com.software.car.api.ModelService;
import com.software.car.common.enums.ModelOptionStateEnum;
import com.software.car.common.enums.ModelOptionTypeEnum;
import com.software.car.common.enums.ModelTypeEnum;
import com.software.car.common.enums.SimpleStateEnum;
import com.software.car.common.utils.CommonPage;
import com.software.car.common.utils.SnowflakeIdGenerator;
import com.software.car.entity.dto.request.InsertModelRequest;
import com.software.car.entity.dto.request.ModelOptionRequest;
import com.software.car.entity.dto.response.ModelOptionResponse;
import com.software.car.entity.dto.response.ModelResponse;
import com.software.car.entity.dto.response.ModelSimpleResponse;
import com.software.car.entity.dto.response.OptionValueResponse;
import com.software.car.entity.po.ModelPo;
import com.software.car.entity.po.OptionValuePo;
import com.software.car.mapper.ModelMapper;
import com.software.car.mapper.ModelOptionMapper;
import com.software.car.mapper.OptionValueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 魔法はまだ解けない
 * @Date 2021/2/22
 */
@Service
public class ModelServiceImpl implements ModelService {
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    ModelOptionMapper modelOptionMapper;

    @Autowired
    OptionValueMapper optionValueMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addModel(InsertModelRequest modelDto) {
        String modelId = SnowflakeIdGenerator.getId();
        modelDto.setId(modelId);

        List<ModelOptionRequest> option = modelDto.getOption();
        option.stream().forEach(modelOption->{
            String modelOptionId = SnowflakeIdGenerator.getId();
            modelOption.setId(modelOptionId);
            modelOption.setModelId(modelId);
            modelOption.setState(1);
            if(modelOption.getType()==2){

                List<OptionValuePo> optionValuePoList = modelOption.getOptionValuePoList();
                for (OptionValuePo optionValuePo: optionValuePoList){
                    optionValuePo.setId(SnowflakeIdGenerator.getId());
                    optionValuePo.setOptionId(modelOptionId);
                }

                optionValueMapper.addValue(optionValuePoList);
            }

        });
        modelOptionMapper.addOptions(option);
        modelMapper.addModel(modelDto);
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateModel(InsertModelRequest modelDto) {
        modelMapper.updateModel(modelDto);
        List<ModelOptionRequest> option = modelDto.getOption();


        return 1;
    }

    @Override
    public CommonPage getSimpleModel(Integer state, Integer type, Integer page, Integer pageSize) {

        PageHelper.startPage(page, pageSize);
        List<ModelSimpleResponse> simpleModel = getSimpleModel(state, type);
        CommonPage wrap = CommonPage.wrap(simpleModel);
        return wrap;
    }

    @Override
    public ModelResponse getModel(String modelId) {
        ModelResponse model = modelMapper.getModelDetail(modelId);
        model.setStateName(SimpleStateEnum.getDescription(model.getState()));
        model.setTypeName(ModelTypeEnum.getDescription(model.getType()));
        return model;

    }

    @Override
    public List<ModelSimpleResponse> getSimpleModel(Integer state, Integer type) {
        List<ModelSimpleResponse> simpleModels = modelMapper.getSimpleModel(state,type);
        simpleModels.stream().forEach(model->{
            model.setStateName(SimpleStateEnum.getDescription(model.getState()));
            model.setTypeName(ModelTypeEnum.getDescription(model.getType()));
        });
        return simpleModels;
    }

    @Override
    public int updateModel(String modelId,Integer state,String updateId) {
        ModelPo build = ModelPo.builder().
                id(modelId).
                updateUser(updateId).
                state(state).
                build();
        modelMapper.updateModel(build);
        return 0;
    }

    @Override
    public List<ModelOptionResponse> getModelOptions(String modelId,Integer state) {
        List<ModelOptionResponse> modelOptions = modelOptionMapper.getModelOptions(modelId,state);
        modelOptions.stream().forEach(model->{
            //根据类型type和状态state，获取中文
            model.setStateName(ModelOptionStateEnum.getDescription(model.getState()));
            model.setTypeName(ModelOptionTypeEnum.getDescription(model.getType()));
            //判断类型，单选则添加子选项
            if(ModelOptionTypeEnum.isSingleChoose(model.getType())){
                //获取单选列表
                List<OptionValueResponse> optionValueList = optionValueMapper.getOptionValueList(model.getId());
                model.setOptionValuePoList(optionValueList);
            }
        });

        return modelOptions;
    }

}
